﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sealight.Core.Domain;
using Sealight.API.Framework;
using NHibernate;
using NHibernate.Linq;

namespace Sealight.Data
{
    public class ProjectDataRepository : NHibernateRepository<ProjectEntity>, IProjectDataRepository
    {
        public IList<ProjectCategoryEntity> GetAllCategory()
        {
            IList<ProjectCategoryEntity> results = null;
            try
            {
                results = UnitOfWork.CurrentSession.CreateCriteria(typeof(ProjectCategoryEntity))
                    .List<ProjectCategoryEntity>();
            }
            catch (ADOException ex)
            {
                //log4net

                //must close session on error
                //if (UnitOfWork.CurrentSession != null && UnitOfWork.CurrentSession.IsOpen)
                //    UnitOfWork.CurrentSession.Close();
                //rethrow the exception
                throw;
            }

            return results;
        }

        public ProjectCategoryEntity GetCategory(Guid id)
        {
            ProjectCategoryEntity results = null;
            try
            {
                results = UnitOfWork.CurrentSession.Get<ProjectCategoryEntity>(id);
            }
            catch (ADOException ex)
            {
               
                //rethrow the exception
                throw;
            }
            return results;
        }

        public IList<ProjectEntity> GetProjectByCategoryId(Guid id)
        {
            IList<ProjectEntity> result = null;
            try
            {
                IQueryable<ProjectEntity> data = UnitOfWork.CurrentSession.Query<ProjectEntity>();
                result = data.Where(c => c.ProjectCategory.ProjectCategoryID == id).ToList();
            }
            catch (ADOException ex)
            {
                //rethrow the exception
                throw;
            }
            return result;
        }
    }
}
